package itsm.isperp.module.controller.incident;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.incident.IcdCategoryPropertySetting;
import itsm.isperp.module.service.incident.IcdCategoryPropertySettingService;
import itsm.isperp.module.utils.JsoupUtils;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-09-09 09:47
 */
@Controller
@RequestMapping("incident/categoryPropertySetting")
public class IcdCategoryPropertySettingController extends BaseController<IcdCategoryPropertySetting> {

	@Autowired
	protected IcdCategoryPropertySettingService icdCategoryPropertySettingService;

	@Override
	public IcdCategoryPropertySettingService getService() {
		return this.icdCategoryPropertySettingService;
	}

	@Override
	public String getPageTitle() {
		return "事件分类扩展属性设置";
	}

	@RequestMapping("settingEdit")
	public ModelAndView settingEdit(String categoryId, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("incident/category/property/settingEdit");
		IcdCategoryPropertySetting model = this.getService().findOne(categoryId);
		if (model == null) {
			model = new IcdCategoryPropertySetting();
			model.setId(categoryId);
			model.setCategoryId(categoryId);
		}
		mv.addObject("model", model);
		return mv;
	}

	@RequestMapping("testEdit")
	public ModelAndView testEdit(String categoryId, HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("incident/category/property/testEdit");
		return mv;
	}

	@Override
	@RequestMapping("save")
	@ResponseBody
	public ResultMessage save(IcdCategoryPropertySetting model, HttpServletRequest req) {

		JdbcTemplate jdbcTemplate = ContextHolder.getSpringBean("jdbcTemplate");
		jdbcTemplate.execute("delete from icd_category_property_setting where id='" + model.getId() + "'");
		this.getService().saveOrUpdate(model);
		return new ResultMessage(true, "success");

	}

	@RequestMapping("formatHtmlStr")
	@ResponseBody
	public String formatHtmlStr(String htmlStr, HttpServletRequest req) {
		if (StringUtils.isNotEmpty(htmlStr))
			return JsoupUtils.formatHtmlStr(htmlStr);
		else
			return "";
	}

}
